package com.ruoyi.web.exams.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.exams.domain.ExamPagerQuestions;
import com.ruoyi.web.exams.dto.PagerQuestionsBatchVo;
import com.ruoyi.web.exams.service.IExamPagerQuestionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 试卷题目关联Controller
 *
 * @author 范书铭
 * @date 2024-10-02
 */
@Api("试卷题目关联Controller")

@RestController
@RequestMapping("/exams/questions")
public class ExamPagerQuestionsController extends BaseController {
    @Autowired
    private IExamPagerQuestionsService examPagerQuestionsService;

    /**
     * 查询试卷题目关联列表
     */
    @ApiOperation("查询试卷题目关联列表")
    @PreAuthorize("@ss.hasPermi('exams:questions:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamPagerQuestions examPagerQuestions) {
        startPage();
        List<ExamPagerQuestions> list = examPagerQuestionsService.selectExamPagerQuestionsList(examPagerQuestions);
        return getDataTable(list);
    }

    /**
     * 导出试卷题目关联列表
     */
    @ApiOperation("导出试卷题目关联列表")
    @PreAuthorize("@ss.hasPermi('exams:questions:export')")
    @Log(title = "试卷题目关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamPagerQuestions examPagerQuestions) {
        List<ExamPagerQuestions> list = examPagerQuestionsService.selectExamPagerQuestionsList(examPagerQuestions);
        ExcelUtil<ExamPagerQuestions> util = new ExcelUtil<ExamPagerQuestions>(ExamPagerQuestions.class);
        util.exportExcel(response, list, "试卷题目关联数据");
    }

    /**
     * 获取试卷题目关联详细信息
     */
    @ApiOperation("获取试卷题目关联详细信息")
    @PreAuthorize("@ss.hasPermi('exams:questions:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(examPagerQuestionsService.selectExamPagerQuestionsById(id));
    }

    /**
     * 新增试卷题目关联
     */
    @ApiOperation("新增试卷题目关联")
    @PreAuthorize("@ss.hasPermi('exams:questions:add')")
    @Log(title = "试卷题目关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamPagerQuestions examPagerQuestions) {
        return toAjax(examPagerQuestionsService.insertExamPagerQuestions(examPagerQuestions));
    }

    /**
     * 修改试卷题目关联
     */
    @ApiOperation("修改试卷题目关联")
    @PreAuthorize("@ss.hasPermi('exams:questions:edit')")
    @Log(title = "试卷题目关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamPagerQuestions examPagerQuestions) {
        return toAjax(examPagerQuestionsService.updateExamPagerQuestions(examPagerQuestions));
    }

    /**
     * 删除试卷题目关联
     */
    @ApiOperation("删除试卷题目关联")
    @PreAuthorize("@ss.hasPermi('exams:questions:remove')")
    @Log(title = "试卷题目关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examPagerQuestionsService.deleteExamPagerQuestionsByIds(ids));
    }

    @ApiOperation("批量添加题目")
    @PreAuthorize("@ss.hasPermi('exams:questions:add')")
    @Log(title = "批量添加题目", businessType = BusinessType.INSERT)
    @PostMapping("/addQuestionBatch")
    public AjaxResult addQuestionBatch(@RequestBody PagerQuestionsBatchVo examPagerQuestions) {
        return toAjax(examPagerQuestionsService.addQuestionsBatch(examPagerQuestions));
    }

    @ApiOperation("批量删除题目")
    @PreAuthorize("@ss.hasPermi('exams:questions:remove')")
    @Log(title = "批量删除题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteQuestionsBatch")
    public AjaxResult deleteQuestions(@RequestBody PagerQuestionsBatchVo pagerQuestionsBatchVo) {
        return toAjax(examPagerQuestionsService.deleteQuestions(pagerQuestionsBatchVo));
    }
}
